package com.nf.xiaomi.util;



import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;

import java.util.Random;

public class SendSmsUtils {
    public static String SendSms(Long phone){
        String result = null;
        try {

            Credential cred = new Credential("AKIDnTxEI7mGWXKsCJCcpnX2IK8h294mQs9C", "rUv89wWwBE4h8MM9k9LM9j5DNbp0697w");

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setReqMethod("POST");
            httpProfile.setConnTimeout(60);
            httpProfile.setEndpoint("sms.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setSignMethod("HmacSHA256");
            clientProfile.setHttpProfile(httpProfile);
            SmsClient client = new SmsClient(cred, "ap-guangzhou",clientProfile);
            SendSmsRequest req = new SendSmsRequest();

            String sdkAppId = "1400777555";
            req.setSmsSdkAppId(sdkAppId);

            String signName = "程序员余生公众号";
            req.setSignName(signName);

            String templateId = "1632047";
            req.setTemplateId(templateId);

            String random = String.format("%04d",new Random().nextInt(999999));
            result = random;
            String[] templateParamSet = {random};
            req.setTemplateParamSet(templateParamSet);

//            String[] phoneNumberSet = {"+8615812792246"};
            String[] phoneNumberSet = {"+86"+phone};
            req.setPhoneNumberSet(phoneNumberSet);

            String sessionContext = "";
            req.setSessionContext(sessionContext);

            String extendCode = "";
            req.setExtendCode(extendCode);

            String senderid = "";
            req.setSenderId(senderid);

            SendSmsResponse res = client.SendSms(req);

            System.out.println(SendSmsResponse.toJsonString(res));
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
        }
        return result;
    }
}
